﻿'    This file is part of The Global Distribution System.

'    The Global Distribution System is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.

'    The Global Distribution System is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with The Global Distribution System.  If not, see <http://www.gnu.org/licenses/>.

Imports System.Text.RegularExpressions

Public Class Form_IP

    Private Sub Form_IP_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If My.Settings.RemoteIP <> String.Empty Then tb_IP.Text = My.Settings.RemoteIP Else tb_IP.Text = "0.0.0.0"
        cb_auto.Checked = My.Settings.AutoConnect
    End Sub

    Private Function IsIPv4(ByVal ip As String) As Boolean
        Dim rIP As Regex = New Regex("^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")
        Return rIP.IsMatch(ip)
    End Function

    Private Sub tb_IP_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tb_IP.TextChanged
        btn_save.Enabled = IsIPv4(tb_IP.Text)
        tb_IP.BackColor = If(IsIPv4(tb_IP.Text), Drawing.Color.White, Drawing.Color.Red)
    End Sub

    Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click
        My.Settings.RemoteIP = tb_IP.Text
        My.Settings.Save()
        Dim MF As New MainForm
        MF.Show()
        Me.Close()
    End Sub

End Class